package com.example.fireassistant_server.service.impl;


import com.example.fireassistant_server.entity.TmpSecret;
import com.example.fireassistant_server.exception.BizException;
import com.example.fireassistant_server.exception.ExceptionType;
import com.example.fireassistant_server.service.StorageService;
import com.tencent.cloud.CosStsClient;
import com.tencent.cloud.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.TreeMap;

@Service("COS")
public class CosStorageServiceImpl implements StorageService {

    @Value("${cos.bucket}")
    private String bucket;

    @Value("${cos.secret-id}")
    private String secretId;

    @Value("${cos.secret-key}")
    private String secretKey;

    @Value("${cos.region}")
    private String region;


    @Override
    public TmpSecret initFileUpload() {
        try {
            Response response = CosStsClient.getCredential(getCosStsConfig());
            TmpSecret tmpSecret = new TmpSecret();
            tmpSecret.setSecretId(response.credentials.tmpSecretId);
            tmpSecret.setSecretKey(response.credentials.tmpSecretKey);
            tmpSecret.setSessionToken(response.credentials.sessionToken);
            tmpSecret.setStartTime(response.startTime);
            tmpSecret.setExpiredTime(response.expiredTime);
            return tmpSecret;
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException(ExceptionType.INNER_ERROR);
        }

    }

    private TreeMap<String, Object> getCosStsConfig() {
        TreeMap<String, Object> config = new TreeMap<String, Object>();
        config.put("secretId", secretId);
        config.put("secretKey", secretKey);
        // 临时密钥有效时长，单位是秒
        config.put("durationSeconds", 1800);
        config.put("allowPrefixes", new String[]{
                "*"
        });
        config.put("bucket", bucket);
        config.put("region", region);
        String[] allowActions = new String[]{
                // 简单上传
                "name/cos:PutObject",
                "name/cos:PostObject",
                // 分片上传
                "name/cos:InitiateMultipartUpload",
                "name/cos:ListMultipartUploads",
                "name/cos:ListParts",
                "name/cos:UploadPart",
                "name/cos:GetObject",
                "name/cos:DeleteObject",
                "name/cos:CompleteMultipartUpload"
        };
        config.put("allowActions", allowActions);
        return config;
    }

}
